/*function loadTable(){
    $.ajax({
        url:"/webapi/student/list"
    }).done(function (rs){
        var len = rs.length;
        var html="";
        for (var i = 0; i < len; i++) {
            var item = rs[i];

            html+="<tr>"

                +"<th>" + item.id + "</th>"
                +"<td>" + item.name + "</td>"
                +"<td>" + item.num + "</td>"
                +"<td>" + item.sex + "</td>"
                +"<td>" + item.score + "</td>"
                +"<td><a href='#' onclick='editstudent("+item.id+");' style='color: #a94442'>编辑</a> <a href='#' onclick='deletebyid("+item.id+");' style='color: #a94442'>删除</a></td>"

                +"</tr>";

        }
        $("#studentTb").html(html);
    })
}*/

/*function showtable (name){
    var  url = '/webapi/student/findbyname';
    if (name!=null && name!=""){
        url = '/webapi/student/findbyname'
        url+="?name="+name
    }
    $.ajax({
        url:url
    }).done(function (rs){
        var len = rs.length;
        var html="";
        for (var i = 0; i < len; i++) {
            var item = rs[i];

            html+="<tr>"

                +"<th>" + item.id + "</th>"
                +"<td>" + item.name + "</td>"
                +"<td>" + item.num + "</td>"
                +"<td>" + item.sex + "</td>"
                +"<td>" + item.score + "</td>"
                +"<td><a href='#' onclick='editstudent("+item.id+");'>编辑</a> <a href='#' onclick='deletebyid("+item.id+");'>删除</a></td>"
                +"</tr>";

        }
        $("#studentTb").html(html);
    })
}*/

function savestudent(){
    var id=$("#id").val();

    if(id<1){
        var data = $("#formstudent").serialize();
        //新增
        $.ajax({
            method: "POST",
            url: "/webapi/student/insert",
            data:data
        }).done( function (){
            $('#stuTable').bootstrapTable("destroy");
            loadTable();
            $('#myModal').modal('hide');
        })
    }
    else {
        //更新
        var data = $("#formstudent1").serialize();
        $.ajax({
            method: "PUT",
            url: "/webapi/student/update",
            data:data
        }).done( function (){
            $('#stuTable').bootstrapTable("destroy");
            loadTable();

            $("#myModal1").modal('hide');
        })
    }
}

function editstudent(id){
    $('#myModal1').modal('show');
    $.ajax({
        url:'/webapi/student/get/'+id
    }).done(function (rs){
            $("#id").val(rs.id);
            $("#name2").val(rs.name);
            $("#sno2").val(rs.sno);
            $("#sex2").val(rs.sex);
            $("#score2").val(rs.score);
        }
    )
}

function preadd(){
    $("#id").val(0);//提示新增
}

function deletebyid(id){
    if(confirm("是否确认删除？")){
        $.ajax({
            url:"/webapi/student/delete/"+id,
            method :"delete"
        }).done(function (){
            $('#stuTable').bootstrapTable("destroy");
            loadTable();
        })
    }
}

function saveuser(){

        var staus = document.getElementById("status").value

        var data1 = $("#formuser").serialize();

        if(staus == "1"){
            $.ajax({
                method: "POST",
                url: "/webapi/user/teacher/insert",
                data:data1
            }).done( function (){
            })
        }
        else{
            $.ajax({
                method: "POST",
                url: "/webapi/user/student/insert",
                data:data1
            }).done( function(){
            })
        }
}


function login(){
    var name =$("#username").val();
    var password = $("#password").val();
    $.ajax({
    url:"/webapi/login/user",
    data:{
    username:name,
    password:password,
}}).done(function (rs){
    if (rs == 1){
    window.location.href="/student/list1";
    }
    else if(rs == 2){
    window.location.href="/student/list";
    }
    else {
    alert("出错了，用户名或密码有误！")}
    })
}

function register(){
    var name =$("#username").val();
    var password = $("#password").val();
    $.ajax({
        url:"/webapi/login/user",
        data:{
            username:name,
            password:password,
        }}).done(function (rs){
            window.location.href="/login/register";
    })
}



function detailFormatter(index, row) {
    var html = []
    $.each(row, function (key, value) {
        html.push('<p><b>' + key + ':</b> ' + value + '</p>')
    })
    return html.join('')
}

function operateFormatter(value, row, index) {
    var id = value;
    return [
        '<button type="button" style="background: #2aabd2;color: white;font-weight: bold" οnclick=\\"editstudent(\'" + id + "\')\\" class="update" title="Update">编辑</button>',
        '<a href="#"> </a>',
        '<button type="button" style="background: #a94442;color: white;font-weight: bold" οnclick=\\"deletebyid(\'" + id + "\')\\" class="remove"  title="Remove">删除</button>'
    ].join('')
}

/*function operateFormatter(value,row,index) {
                 var result = "";
                 id = row.id;
                 result += "<a href='javascript:;'  class='btn btn-xs blue' onClick=\"editstudent(id);\" title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                 result += "<a href='javascript:;' class='btn btn-xs red' onClick=\"deletebyid(id);\" title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
                 return result;
}*/

window.operateEvents = {
    'click .update': function (e, value, row, index) {
        $("#stuTable").bootstrapTable(onclick(editstudent(row.id)))
    },
    'click .remove': function (e, value, row, index) {
        $("#stuTable").bootstrapTable(onclick(deletebyid(row.id)))
    }
};

function discriminatesex(){
    var str = $("#searchmessage").val()
    if (str == "1" || str == "2") {
        return $("#searchmessage").val()
    }
    else{
        return null
    }
}

function discriminatescore(){
    var str = $("#searchmessage").val()
    var han = /^[\u4e00-\u9fa5]+$/;
    if (!han.test(str) && str != "1" && str != "2") {
        return $("#searchmessage").val()
    }
    else{
        return null
    }
}

function discriminatename(){
    var str = $("#searchmessage").val()
    var han = /^[\u4e00-\u9fa5]+$/;
    if (!han.test(str)) {
        return null
    }
    else{
        return $("#searchmessage").val()
    }
}

function loadTable(){
    $('#stuTable').bootstrapTable({
        url: '/webapi/student/getbypage',
        striped:true,//设置为true会隔行变色
        pagination:true,//设置为true底部会显示页码
        singleSelect:false,//设置为true将会禁止多选
        pageSize: 4,//每页数据条数
        pageNumber: 1,//首页页码
        pageList: [4,6,8,10,15],
        sidePagination:"server",//设置在哪里分页可在server或者client
        queryParams: function (params){
            var paraObj = {
                size: params.limit,
                page: params.offset/params.limit,
                sort:"id",
                direct:"desc",
                sex:discriminatesex(),
                score: discriminatescore(),
                name:discriminatename()
            };
            return paraObj
        },
        columns: [
            [{
                title: '序号',
                field: 'id',
                rowspan: 2,
                align: 'center',
                valign: 'middle',
                sortable: true,
            }, {
                title: '个人信息',
                colspan: 5,
                align: 'center'
            }],[{
                field: 'name',
                title: '姓名',
                sortable: true,
                align: 'center'
            },{
                field: 'sno',
                title: '学号',
                sortable: true,
                align: 'center',
            },{
                field: 'sex',
                title: '性别',
                sortable: true,
                align: 'center',
            },{
                field: 'score',
                title: '成绩',
                sortable: true,
                align: 'center',
            },{
                field: 'operate',
                title: '操作',
                align: 'center',
                clickToSelect: true,
                events: operateEvents,
                formatter: operateFormatter("id")
            }]],
            responseHandler:function (res){
        return{
            "total":res.data.totalElements,
            "rows":res.data.content
        };
    }
    });
}//end of loadtable


function studentTable(){
    $('#stuTable1').bootstrapTable({
        url: '/webapi/student/getbypage1',
        striped:true,//设置为true会隔行变色
        pagination:true,//设置为true底部会显示页码
        singleSelect:false,//设置为true将会禁止多选
        pageSize: 4,//每页数据条数
        pageNumber: 1,//首页页码
        pageList: [4,6,8,10,15],
        sidePagination:"server",//设置在哪里分页可在server或者client
        queryParams: function (params){
            var paraObj = {
                size: params.limit,
                page: params.offset/params.limit,
                sort:"id",
                direct:"desc",
            };
            return paraObj
        },
        columns: [
            [{
                title: '序号',
                field: 'id',
                rowspan: 2,
                align: 'center',
                valign: 'middle',
                sortable: true,
            }, {
                title: '课程信息',
                colspan: 5,
                align: 'center'
            }],[{
                field: 'cname',
                title: '课程名',
                sortable: true,
                align: 'center'
            },{
                field: 'cno',
                title: '课程编号',
                sortable: true,
                align: 'center',
            },{
                field: 'credit',
                title: '课程学分',
                sortable: true,
                align: 'center',
            },{
                field: 'firstscore',
                title: '成绩',
                sortable: true,
                align: 'center',
            }, {
                field: 'secondscore',
                title: '补考成绩',
                sortable: true,
                align: 'center',
            }]],
        responseHandler:function (res){
            return{
                "total":res.data.totalElements,
                "rows":res.data.content
            };
        }
    });
}

function showTable(){
    $('#stuTable').bootstrapTable({
        url: '/webapi/student/getbypage',
        method:'GET',
        cache:false,
        sortable:false,
        pageList:[4,6,8,10,15,20],
        strictSearch:true,
        uniqueId:"id",
        striped:true,//设置为true会隔行变色
        pagination:true,//设置为true底部会显示页码
        singleSelect:false,//设置为true将会禁止多选
        pageSize: 4,//每页数据条数
        pageNumber: 1,//首页页码
        sidePagination:"server",//设置在哪里分页可在server或者client
        queryParams: function (params){
            var paraObj = {
                size: params.limit,
                page: params.offset/params.limit,
                sort:"id",
                direct:"desc",
                name:"",
                score: ""
            };
            return paraObj
        },
        columns: [
            [{
                title: '序号',
                field: 'id',
                rowspan: 2,
                align: 'center',
                valign: 'middle',
                sortable: true,
            },{
                title: '个人信息',
                colspan: 5,
                align: 'center'
            }],[{
                    field: 'name',
                    title: '姓名',
                    sortable: true,
                    align: 'center'
                },{
                    field: 'sno',
                    title: '学号',
                    sortable: true,
                    align: 'center',
                },{
                    field: 'sex',
                    title: '性别',
                    sortable: true,
                    align: 'center',
                },{
                    field: 'score',
                    title: '成绩',
                    sortable: true,
                    align: 'center',
                },{
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    clickToSelect: false,
                    events: operateEvents,
                    formatter: operateFormatter("id")
                }]],
            responseHandler:function (res){
                return{
                "total":res.data.totalElements,
                "rows":res.data.content
            };
        }
    });

}//end of loadtable


function search(){
    $('#stuTable').bootstrapTable("destroy");

    loadTable();
}

function goback(){
    var name = ("");
    $("#name").val("");
    showTable()
}

